# @Time:   2021/2/7 9:55
# @Author: Zong Wei


from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class RoutePage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='线路管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")

    customer = (By.XPATH, "//td/select[@id='Customer_Name']/following::a[1]")
    cus_val = (By.XPATH, "//form[@id='form1']/following::tr[@id='datagrid-row-r1-2-2']")
    customer1 = (By.XPATH, "//div/select[@id='Customer_Name']/following::a[1]")
    cus_val1 = (By.XPATH, "//div[@id='layout']/following::tr[@id='datagrid-row-r1-2-2']")

    truck = (By.XPATH, "//td/select[@id='Customer_Name']/following::a[2]")
    truck_val = (By.XPATH, "//form[@id='form1']/following::tr[@id='datagrid-row-r2-2-2']")
    truck1 = (By.XPATH, "//div/select[@id='Customer_Name']/following::a[2]")
    truck_val1 = (By.XPATH, "//div[@id='layout']/following::tr[@id='datagrid-row-r2-2-3']")

    get_tk_address = (By.XPATH, "//td/select[@id='Customer_Name']/following::a[3]")
    get_tk_val = (By.XPATH, "//form[@id='form1']/following::tr[@id='datagrid-row-r3-2-0']")
    pick_up_address = (By.XPATH, "//td/select[@id='Customer_Name']/following::a[4]")
    pick_up_val = (By.XPATH, "//form[@id='form1']/following::tr[@id='datagrid-row-r5-2-1']")
    delivery_address = (By.XPATH, "//td/select[@id='Customer_Name']/following::a[5]")
    delivery_val = (By.XPATH, "//form[@id='form1']/following::tr[@id='datagrid-row-r4-2-2']")
    return_tk_address = (By.XPATH, "//td/select[@id='Customer_Name']/following::a[6]")
    return_val = (By.XPATH, "//form[@id='form1']/following::tr[@id='datagrid-row-r6-2-3']")

    trans_type = (By.ID, "UesFor")
    sub_dept = (By.ID, "Job_TypeName")
    loading_require = (By.ID, "loadingRequire")
    unloading_require = (By.ID, "unloadingRequire")

    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='业务模式']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def route_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.driver.execute_script('$("#admui-navTabs").scrollTop(1000)')
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_route(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加路线")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.customer)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.cus_val)).click()

        self.wait.until(ec.presence_of_element_located(self.truck)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.truck_val)).click()
        self.wait.until(ec.element_to_be_clickable(self.get_tk_address)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.get_tk_val)).click()

        self.wait.until(ec.element_to_be_clickable(self.pick_up_address)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.pick_up_val)).click()

        self.wait.until(ec.element_to_be_clickable(self.delivery_address)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.delivery_val)).click()

        self.wait.until(ec.element_to_be_clickable(self.return_tk_address)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.return_val)).click()

        Select(self.driver.find_element(*self.trans_type)).select_by_index(1)
        Select(self.driver.find_element(*self.sub_dept)).select_by_index(2)

        self.wait.until(ec.presence_of_element_located(self.loading_require)).send_keys(Faker().address())
        self.wait.until(ec.presence_of_element_located(self.unloading_require)).send_keys(Faker().address())
        sleep(1)
        self.driver.switch_to.default_content()
        # self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_route(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.customer1)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.cus_val1)).click()

        self.wait.until(ec.element_to_be_clickable(self.truck1)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.truck_val1)).click()
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_route(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        # self.search_route()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.loading_require)).send_keys(Faker().address())
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_route(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        # self.search_route()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        # self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = RoutePage()
    pg.route_home()
    pg.add_route()

